package com.linxiao.payment.business.impl.ali;

import com.alibaba.fastjson2.JSONObject;
import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliTransferOrder;
import com.egzosn.pay.ali.bean.AliTransferType;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.linxiao.payment.business.TransferManager;
import com.linxiao.payment.vo.TransferRequest;
import com.linxiao.payment.core.constants.IfCodeEnum;
import com.linxiao.payment.web.domain.PayInterfaceConfig;
import com.linxiao.payment.web.service.IPayInterfaceConfigService;
import com.ruoyi.common.exception.base.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 支付宝转账
 * @author zhoudong
 */
@Component
public class AliPayTransferManager implements TransferManager {
    private static final Logger LOG = LoggerFactory.getLogger(AliPayTransferManager.class);
    @Resource
    private AutowireCapableBeanFactory spring;
    @Resource
    private IPayInterfaceConfigService payInterfaceConfigService;

    /**
     * 转账
     * @param transferRequest 支付宝转账实体
     */
    public Map<String, Object> transfer(TransferRequest transferRequest) {
        AliPayService service = init(transferRequest.getMchId());
        AliTransferOrder order = new AliTransferOrder();
        order.setOutBizNo(transferRequest.getOutBizNo());
        order.setTransAmount(transferRequest.getTransAmount());
        order.setOrderTitle(transferRequest.getTitle());
        order.setIdentity(transferRequest.getMobile());
        order.setIdentityType("ALIPAY_LOGON_ID");
        order.setName(transferRequest.getName());
        order.setRemark(transferRequest.getRemark());
        //单笔无密转账到支付宝账户
        order.setTransferType(AliTransferType.TRANS_ACCOUNT_NO_PWD);
        Map<String, Object> transfer = service.transfer(order);
        LOG.info("支付宝转账，返回：{}", transfer);
        return transfer;
    }

    /**
     * 初始化商户
     *
     * @return
     * @param mchId
     */
    private AliPayService init(String mchId) {
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.selectByMchIdAndIfCode(Long.parseLong(mchId), IfCodeEnum.ALIPAY_TRANSFER);
        if (payInterfaceConfig == null) {
            throw new BaseException("当前商户未配置支付宝出款信息，商户ID：" + mchId);
        }
        JSONObject params = JSONObject.parseObject(payInterfaceConfig.getIfParams());
        AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
        aliPayConfigStorage.setPid(params.getString("mchId"));
        aliPayConfigStorage.setAppId(params.getString("appId"));
//        aliPayConfigStorage.setAppAuthToken("ISV代商户代用，指定appAuthToken");
        //普通公钥方式与证书公钥方式为两者取其一的方式
        aliPayConfigStorage.setKeyPrivate(params.getString("privateKey"));
        aliPayConfigStorage.setKeyPrivate(params.getString("alipayPublicKey"));
        aliPayConfigStorage.setSignType(params.getString("signType"));
        aliPayConfigStorage.setSeller(params.getString("mchId"));
        aliPayConfigStorage.setInputCharset(StandardCharsets.UTF_8.name());

        //请求连接池配置
        HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
        //最大连接数
        httpConfigStorage.setMaxTotal(2);
        //默认的每个路由的最大连接数
        httpConfigStorage.setDefaultMaxPerRoute(1);
        return new AliPayService(aliPayConfigStorage, httpConfigStorage);
    }
}
